﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace ProjectNocturnalFool
{
    class ParticleEngine
    {
        private Random random;
        public Vector2 EmitterLocation { get; set; }
        private List<Particle> particles;
        private List<Texture2D> textures;

        public ParticleEngine(List<Texture2D> textures)
        {
            this.textures = textures;
            this.particles = new List<Particle>();
            random = new Random();
        }

        public void Update()
        {
            for (int particle = 0; particle < particles.Count; particle++)
            {
                particles[particle].Update();
                if (particles[particle].TTL <= 0)
                {
                    particles.RemoveAt(particle);
                    particle--;
                }
            }
        }

        public void GenerateNewParticle(int x, int y)
        {
            Texture2D texture = textures[random.Next(textures.Count)];
            Vector2 position = EmitterLocation;
            position.X = x;
            position.Y = y;
            Vector2 direction = new Vector2(
                                    1f * (float)(random.NextDouble() * 2 - 1),
                                    1f * (float)(random.NextDouble() * 2 - 1));
            direction.Normalize();

            float angle = 0;
            float angularVelocity = 0.1f * (float)(random.NextDouble() * 2 - 1);
            Color color = new Color(
                        (float)random.NextDouble(),
                        (float)random.NextDouble(),
                        (float)random.NextDouble());
            float size = (float)random.NextDouble();
            int ttl = 20 + random.Next(40);

            float maxVel = 5f ;
            float lowPassSpeed = 0.1f;
            Velocity vel = new Velocity((float)(random.NextDouble()) * maxVel + maxVel, 0f, lowPassSpeed);

            particles.Add(new Particle(texture, position, direction, angle, angularVelocity, color, size, ttl, vel));
        }

        public void GenerateNewBurstParticle(int x, int y)
        {
            for(int i = 0; i < random.Next(15,30); i++)
            {
                GenerateNewParticle(x,y);
            }
        }

        public void GenerateNewFallingParticle()
        {
            Texture2D texture = textures[random.Next(textures.Count)];
            Vector2 position = EmitterLocation;
            position.X = random.Next(VirtualScreen.Instance.VirtualWidth);
            position.Y = 0;
            Vector2 direction = new Vector2(
                                    1f * (float)(random.NextDouble() * 2 - 1),
                                    1);
            direction.Normalize();

            float angle = 0;
            float angularVelocity = 0.1f * (float)(random.NextDouble() * 2 - 1);
            Color color = new Color(
                        (float)random.NextDouble(),
                        (float)random.NextDouble(),
                        (float)random.NextDouble());
            float size = (float)random.NextDouble();
            int ttl = 90 + random.Next(40);

            float maxVel = 1f;
            float lowPassSpeed = 0.005f;
            Velocity vel = new Velocity((float)(random.NextDouble()) * maxVel + maxVel, 0f, lowPassSpeed);

            particles.Add(new Particle(texture, position, direction, angle, angularVelocity, color, size, ttl, vel));
        }

        public void Draw()
        {
            TacticsGame.Instance.spriteBatch.Begin();
            for (int index = 0; index < particles.Count; index++)
            {
                particles[index].Draw();
            }
            TacticsGame.Instance.spriteBatch.End();
        }
    }
}
